package com.cskaoyan.day5;

public class RemoveStrngElement {
    public static void main(String[] args) {
        String[] st = {"abc", "123", "123", "123", "666", "777"};
        int removeCount = removeStrngElement(st, "123");
        for (int i = 0; i < st.length - removeCount; i++) {
            System.out.printf(st[i] + "\t");
        }

        System.out.println();
        String[] st1 = {"abc", null, "123", "123", null, null};
        int removeCount1 = removeStrngElement(st1, null);
        for (int i = 0; i < st1.length - removeCount1; i++) {
            System.out.printf(st1[i] + "\t");
        }

    }

    public static int removeStrngElement(String[] old, String target) {
        int count = 0;
        for (int i = 0; i < old.length - count; ) {
            if (target == null) {
                if (old[i] == null) {
                    for (int j = i; j < old.length - count - 1; j++) {
                        old[j] = old[j + 1];
                    }
                    count++;
                    continue;
                }
            } else if (target.equals(old[i])) {
                for (int j = i; j < old.length - count - 1; j++) {
                    old[j] = old[j + 1];
                }
                count++;
                continue;
            }
            i++;
        }
        return count;
    }
}
